﻿using Lcygcc.Domain.Common;
using Lcygcc.Domain.Repository.Entity.JKModel;
using Lcygcc.Domain.Service;
using Lcygcc.Infrastructure.Core;
using Lcygcc.Infrastructure.Logs;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lcygcc.Domain.TaskServiceAdapter
{
    /// <summary>
    /// 同步金科卡系列
    /// </summary>
    public class QueryCardSeriesAdapter : ITaskWork
    {
        private readonly ICardSeriesService mCardSeriesService = null;

        public QueryCardSeriesAdapter(ICardSeriesService cardSeriesService)
        {
            mCardSeriesService = cardSeriesService;
        }
        public void Execute(RequestArgs args)
        {
            try
            {
                string sendUrl = "";
                var detail = CommonHelper.GetDicDetailInfo("enmPlateformToUrl", enumServiceID.queryCardSeriesList.ToString());
                if (detail != null)
                {
                    sendUrl = Path.Combine(EnvironmentInfo.JKBaseUrl, detail.NisspCode).Replace("\\", "/");

                    string reqParam = "";
                    string respones = HttpService.PostRequest(sendUrl, Encoding.UTF8, reqParam);
                    if (!string.IsNullOrEmpty(respones))
                    {
                        JKGetCardSeriesResponse model = JsonConvert.DeserializeObject<JKGetCardSeriesResponse>(respones);
                        if (model != null)
                        {
                            if (model.code == (int)enumJKReponse.OK)
                            {
                                List<CardSeries> list = model.list;
                                if (list.Count > 0)
                                {
                                    mCardSeriesService.Save(list);
                                }
                            }
                        }
                        else
                        {
                            Logger.Info("-----QueryCardSeriesAdapter.Execute-----:" + "JKGetCardSeriesResponse反序列化为空！");
                        }
                    }
                    else
                    {

                    }
                }
                else
                {
                    Logger.Info("-----QueryCardSeriesAdapter.Execute-----:" + "找不到对应的ServiceId：" + enumServiceID.queryCardSeriesList.ToString());
                }
            }
            catch (Exception ex)
            {
                Logger.Error("----查询卡系列QueryCardSeriesAdapter-----", ex);
            }
        }
    }
}
